package com.geek.javase.ch10.work.listwork.work2;

import java.util.LinkedList;

/** 练习2：用LinkedList集合，模拟堆栈操作(存入的元素是Person对象)  先进后出
 */
public class Stack {
    private LinkedList<Person> stack;

    public Stack() {
        stack = new LinkedList<>();
    }

    // 压栈操作
    public void push(Person person) {
        stack.addFirst(person);
    }

    // 弹栈操作
    public Person pop() {
        if (isEmpty()) {
            throw new RuntimeException("Stack is empty");
        }
        return stack.removeFirst();
    }

    // 判断栈是否为空
    public boolean isEmpty() {
        return stack.isEmpty();
    }

    // 获取栈顶元素（不移除）
    public Person peek() {
        if (isEmpty()) {
            throw new RuntimeException("Stack is empty");
        }
        return stack.getFirst();
    }

    // 获取栈的大小
    public int size() {
        return stack.size();
    }

    @Override
    public String toString() {
        return stack.toString();
    }
}
